package com.css.fxfzypg.modules.surveytaskmanage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.cache.DivisionCache;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.group.vo.GroupVo;
import com.css.fxfzypg.modules.surveytaskmanage.entity.FieldSurveyTaskEntity;
import com.css.fxfzypg.modules.surveytaskmanage.service.FieldSurveyTaskService;
import com.css.fxfzypg.modules.surveytaskmanage.vo.FieldSurveyTaskGroupVo;
import com.css.fxfzypg.modules.surveytaskmanage.vo.FieldSurveyTaskVo;
import com.css.fxfzypg.modules.surveytaskmanage.vo.FieldSurveyVo;
import com.css.fxfzypg.modules.surveytaskmanage.vo.TaskVo;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/surveyTask")
@Slf4j
public class FieldSurveyTaskController {

    @Autowired
    FieldSurveyTaskService fieldSurveyTaskService;

    /**
     * 新建任务
     * @param fieldSurveyTaskVo
     * @return
     */
    @SysLog("新建任务")
    @PostMapping("/save")
    public RestResponse saveTask(@RequestBody FieldSurveyTaskVo fieldSurveyTaskVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String parentTaskId=fieldSurveyTaskService.saveTask(fieldSurveyTaskVo);
            jsonObject.put("message","保存成功");
            jsonObject.put("parentTaskId",parentTaskId);
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 新建子任务
     * @param subTask
     * @return
     */
    @SysLog("新建子任务")
    @PostMapping("/saveSubTask")
    public RestResponse saveSubTask(@RequestBody FieldSurveyTaskEntity subTask){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            fieldSurveyTaskService.saveSubTask(subTask);
            jsonObject.put("message","新建成功");
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="新建失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * app端任务列表查询
     * @param taskVo
     * @return
     */
    @GetMapping("/appPage")
    public RestResponse getPage(HttpServletRequest request, TaskVo taskVo){
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map=fieldSurveyTaskService.getAppPage(request,taskVo,curPage,pageSize);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(map);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 折叠任务列表查询
     * @param taskVo
     * @return
     */
    @SysLog("任务列表查询")
    @GetMapping("/page")
    public RestResponse getPage(TaskVo taskVo,HttpServletRequest request){
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=fieldSurveyTaskService.getPage(taskVo,curPage,pageSize);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 子任务列表查询
     * @param parentTaskId
     * @return
     */
    @SysLog("子任务列表查询")
    @GetMapping("/getSubTask")
    public RestResponse getSubTask(String parentTaskId){
        RestResponse response = null;
        try{
            List<FieldSurveyTaskEntity> subTaskList=fieldSurveyTaskService.getSubTask(parentTaskId);
            response = RestResponse.succeed(subTaskList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 查看任务详情
     * @param id
     * @return
     */
    @SysLog("查看任务详情")
    @GetMapping("/findById")
    public RestResponse findById(String id){
        RestResponse response = null;
        try{
            FieldSurveyTaskGroupVo FieldSurveyTaskEntity=fieldSurveyTaskService.findById(id);
            response = RestResponse.succeed(FieldSurveyTaskEntity);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 修改任务
     * @param
     * @return
     */
    @SysLog("修改任务")
    @PostMapping("/update")
    public RestResponse updateTask(@RequestBody FieldSurveyTaskEntity fieldSurveyTaskEntity){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            fieldSurveyTaskService.updateTask(fieldSurveyTaskEntity);
            jsonObject.put("message","修改成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 结束任务
     * @param
     * @return
     */
    @SysLog("结束任务")
    @PostMapping("/complete/{ids}")
    public RestResponse completeTask(@PathVariable String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] arr= ids.split(",");
            List<String> list = Arrays.asList(arr);
            fieldSurveyTaskService.completeTask(list);
            jsonObject.put("message","结束成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "结束失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 分配小组
     * @param fieldSurveyTaskEntity
     * @return
     */
    @SysLog("分配小组")
    @PostMapping("/assignGroup")
    public RestResponse assignGroup(@RequestBody FieldSurveyTaskEntity fieldSurveyTaskEntity){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            fieldSurveyTaskService.assignGroup(fieldSurveyTaskEntity);
            jsonObject.put("message","分配成功");
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="分配失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 删除任务
     * @param
     * @return
     */
    @SysLog("删除任务")
    @PostMapping("/delete/{ids}")
    public RestResponse deleteTask(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] arr= ids.split(",");
            List<String> list = Arrays.asList(arr);
            fieldSurveyTaskService.deleteTask(list);
            jsonObject.put("message","删除完成");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取任务编号
     * @return
     */
    @GetMapping("/taskCode")
    public RestResponse getTaskCode(String taskYear){
        RestResponse restResponse=null;
        try {
            String taskCode = fieldSurveyTaskService.getTaskCode(taskYear);
            restResponse= RestResponse.succeed(taskCode);
        } catch (Exception e) {
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取子任务编号
     * @return
     */
    @GetMapping("/subTaskCode")
    public RestResponse getSubTaskCode(String taskNum){
        RestResponse restResponse=null;
        try {
            String subtaskCode=fieldSurveyTaskService.getSubTaskCode(taskNum);
            restResponse= RestResponse.succeed(subtaskCode);
        } catch (Exception e) {
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 轨迹查看成员获取
     * @param
     * @return
     */
    @GetMapping("/getActionTrackPersonnel")
    public RestResponse getActionTrackPersonnel(String createUser,String groupId,String taskId){
        RestResponse restResponse=null;
        try {
            List<Map<String,Object>> ActionTrackS=fieldSurveyTaskService.getActionTrackPersonnel(createUser,groupId,taskId);
            restResponse= RestResponse.succeed(ActionTrackS);
        }catch (Exception e){
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 查询所有父任务
     * @param
     * @return
     */
    @GetMapping("/getParentTask")
    public RestResponse getParentTask(){
        RestResponse response = null;
        try{
            List<FieldSurveyTaskEntity> parentTaskList=fieldSurveyTaskService.getParentTask();
            response = RestResponse.succeed(parentTaskList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取未结束的所有任务(用于区县致死性)
     * @return
     */
    @GetMapping("/getTask")
    public RestResponse getTask(){
        RestResponse response = null;
        try{
            List<TaskVo> list = fieldSurveyTaskService.getTask();
            response = RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 设防烈度·修改
     * @param
     * @return
     */
    @PostMapping("/updateTaskIntensity")
    public RestResponse updateTaskIntensity(@RequestParam("taskId") String taskId,@RequestParam("intensity") String intensity){
        RestResponse response = null;
        try{

            Boolean boo=fieldSurveyTaskService.updateTaskIntensity(taskId,intensity);
            if(boo){
                response = RestResponse.succeed("修改成功");
            }else{
                response = RestResponse.succeed("修改失败");
            }
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}